package cn.kai.org.service.impl;

import cn.kai.basic.service.impl.BaseServiceImpl;
import cn.kai.org.domain.Department;
import cn.kai.org.mapper.DepartmentMapper;
import cn.kai.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Kai
 * @Date 2022/6/1 18:20
 * @PackageName:cn.kai.org.service.impl
 * @ClassName: DepartmentServiceImpl
 * @Description: TODO
 * @Version 1.0
 */

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    /**
     * 新增数据
     * @param department
     */
    @Transactional
    @Override
    public void save(Department department) {
        departmentMapper.save(department);
        //在mapper.xml中开启返回自增id后，自增的id会返回至调用的domain对象中，然后再用这个domain对象调用修改方法，去修改dirPath
        this.update(department);
    }

    /**
     * 修改数据
     * @param department
     */
    @Transactional
    @Override
    public void update(Department department) {
        String dirPath = "";
        if(department.getParent() == null){
            //1.新增的是顶级部门  /自己id
            dirPath = "/"+department.getId();
        }else{
            //2.新增的是非顶级部门    找上级部门的dirpath + /自己id
            Department parent = departmentMapper.loadById(department.getParent().getId());
            dirPath = parent.getDirPath() + "/"+department.getId();
        }
        department.setDirPath(dirPath);

        departmentMapper.update(department);
    }

    /**
     * 获取部门树
     * @return
     */
    @Override
    public List<Department> queryTree() {
        List<Department> departments = departmentMapper.loadAll();
        //为了后面找自己的父亲，我们这里可以把整体的部门数据，转换一份成Map<id,department>
        //Map<Long, Department> allDepts = departments.stream().collect(Collectors.toMap(Department::getId, dept -> dept));
        Map<Long, Department> allDepts = new HashMap<>();
        for (Department department : departments) {
            allDepts.put(department.getId(),department);
        }
        //新建一个集合用来存放顶级部门
        List<Department> firstDepts = new ArrayList<>();

        for (Department temp : departments) {
            //如果父级id为null，则为顶级部门
            if (temp.getParent_id()==null){
                firstDepts.add(temp);
            }else {
                //以父类id为key值查出父类元素，因为map中赋值为引用类型Department元素的地址值。所以修改共享有效
                Department parent = allDepts.get(temp.getParent_id());
                if (parent != null){
                    parent.getChildren().add(temp);
                }
            }
        }
        return firstDepts;

        /*//获取所有部门数据
        List<Department> departments = departmentMapper.loadAll();
        //新建一个集合用来存放顶级部门
        List<Department> firstDepts = new ArrayList<>();

        for (Department temp : departments) {
            //如果父级id为null，则为顶级部门
            if (temp.getParent_id()==null){
                firstDepts.add(temp);
            }else {
                //非顶级部门的，就寻找自己的上级部门，并将自己放入上级部门中
                for (Department parent : departments) {
                    //如果自己的父级部门id和该遍历集合元素的id相等，则为自己的父级部门
                    if (temp.getParent_id()==parent.getId()){
                        //因为list中值为引用类型Department元素的地址值。所以修改共享有效
                        parent.getChildren().add(temp);
                        //找到之后，结束当前循环
                        break;
                    }
                }
            }
        }*/
    }
}
